package edu.lambdateacher.awt;

/*
 LambdaTeacher
 Copyright (C) 2000  Kim Mason
 Copyright (C) 2014  Vitalij Zadneprovskij

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

import java.awt.Dialog;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import edu.lambdateacher.expressions.Expression;

/**
 * AlphaDialog is a dialog box which is shown in the event of a name clash. The
 * user enters a new name for the clashing variable.
 */

public class AlphaDialog extends Dialog {
	private String newname = null;
	private AlphaDialogInfo infoshower = null;
	private TextField thefield = null;
	// an event handler for the textfield
	private ActionListener thefieldactionlistener = new ActionListener() {
		public void actionPerformed(ActionEvent event) {
			newname = thefield.getText();
			AlphaDialog.this.setVisible(false);
		}
	};

	/**
	 * param is the parameter with the clashing variable target is the
	 * expression that is param is being subbed into. nameclash is the name of
	 * the variable that is clashing.
	 */
	public AlphaDialog(Frame parent, Expression target, Expression param, String nameclash, String tosubfor) {
		// this is a modal dialog with the title "Name Clash"
		super(parent, "Name Clash", true);
		// create an input field with 16 columns
		thefield = new TextField(16);
		thefield.setEditable(true);
		thefield.addActionListener(thefieldactionlistener);
		infoshower = new AlphaDialogInfo(target, param, nameclash, tosubfor);
		this.add("Center", infoshower);
		this.add("South", thefield);
		this.pack();
	}

	public String getNewName() {
		return newname;
	}
}

/**
 * AlphaDialogInfo puts up information in the AlphaDialog which tells the user
 * about the name clash.
 */

class AlphaDialogInfo extends TextArea {
	private String tooutput = null;

	AlphaDialogInfo(Expression target, Expression param, String nameclash, String tosubfor) {
		super();

		tooutput = "While attempting to substitute the parameter expression\n\n";
		if (param != null)
			tooutput = tooutput + param.stringRepresentation(false);
		tooutput = tooutput + "\n\nfor the variable\n\n";
		if (tosubfor != null)
			tooutput = tooutput + tosubfor;
		tooutput = tooutput + "\n\nin the expression\n\n";
		if (target != null)
			tooutput = tooutput + target.stringRepresentation(false);
		tooutput = tooutput + "\n\nthe variable " + nameclash + " has a name clash.\n";
		tooutput = tooutput + "This means that un-bound variables in the parameter expression have ";
		tooutput = tooutput + "the same name as un-bound variables in the expression being modified.\n";
		tooutput = tooutput + "Please enter a new name for the clashing variable below, and it will\n";
		tooutput = tooutput + "be re-named (alpha reduction) to the new name.  Hit enter when finished.";

		this.setEditable(false);
		this.setText(tooutput);
	}
}
